<?php
namespace app\admin\controller;

use app\common\controller\Back;

class Menu extends Back
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $args = [
            'where' => [],
            'order' => [
                'menu_weight' => 'desc',
                'menu_id' => 'desc',
            ],
        ];
        //状态
        if($this->query['status']){
            if(in_array($this->query['status'],model('common/Attr','loglic')->statusKeys())){
                array_push($args['where'],['menu_status','eq',$this->query['status']]);
            }
        }
        //分组
        if($this->query['group']){
            array_push($args['where'],['menu_group','eq',$this->query['group']]);
        }
        //搜索
        if($this->query['searchText']){
            array_push($args['where'],['menu_name|menu_url','like','%'.$this->query['searchText'].'%']);
        }
        //查询数据
        $data = model('common/Menu','loglic')->allLevel($args);
        //模板变量赋值
        $this->assign($this->site);
        $this->assign('data',$data);
        $this->assign('query',$this->query);
        //渲染模板输出
        return $this->fetch();
    }

    public function create()
    {
        $form = xfFormCreate([
            'name'      => 'adminMenuCreate',
            'action'    => xfUrl('admin/menu/save'),
            'items'     => model('admin/Menu','form')->items([
                'menu_icon'   => 'bi-list',
                'menu_weight' => model('common/Menu','loglic')->getWeightMax(),
            ]),
            'reset'     => '重置',
            'class'     => 'row',
        ]);
        $this->assign('form', $form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function save()
    {
        $model = model('common/Menu','loglic');
        $result = $model->dataSave(input('post.'));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功','admin/menu/index');
    }

    public function delete()
    {
        $result = model('common/Menu','loglic')->deleteId(input('id/d',0));
        if($result){
            $this->success('成功');
        }else{
            $this->error(model('common/Menu','loglic')->getError());
        }
    }

    public function deletes()
    {
        foreach(input('ids/a') as $key=>$typeId){
            model('common/Menu','loglic')->deleteId($typeId);
        }
        $this->success('成功');
    }
    
    public function edit()
    {
        if(!$id = input('id/d',0)){
            $this->error($this->error,'admin/menu/index');
        }
        //查询数据
        $result = model('common/Menu','loglic')->getId($id);
        if(!$result){
            $this->error($this->error,'admin/menu/index');
        }
        $form = xfFormCreate([
            'name'         => 'adminMenuEdit',
            'action'       => xfUrl('admin/menu/update'),
            'items'        => model('admin/Menu','form')->items($result),
            'reset'        => '重置',
            'class'        => 'row',
        ]);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function update()
    {
        $model = model('common/Menu','loglic');
        $result = $model->dataUpdate(input('post.'));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功','admin/menu/index');
    }

    //快速排序
    public function weight()
    {
        $model = model('common/Menu','loglic');
        $result = $model->updateWeight(input('id/d'),input('value/d'));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功');
    }

    //批量审核
    public function normal()
    {
        $result = model('common/Menu','loglic')->updateStatus(input('ids/a'),'normal');

        $this->success('成功');
    }

    //批量隐藏
    public function hidden()
    {
        $result = model('common/Menu','loglic')->updateStatus(input('ids/a'),'hidden');

        $this->success('成功');
    }

    //批量审核
    public function group()
    {
        $result = model('common/Menu','loglic')->updateGroup(input('ids/a'),input('value/d'));

        $this->success('成功');
    }
}